import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import java.util.ArrayList;
import java.util.Iterator;
import java.io.IOException;
@SuppressWarnings("serial")
public class Gridlayout extends JFrame{

	public Gridlayout(int row, int col){

		setLayout(new GridLayout(row,col));
	}
	static ArrayList<Person> perList = new ArrayList<Person>();	
	static ArrayList<Student> stuList = new ArrayList<Student>();
	static ArrayList<Employee> empList = new ArrayList<Employee>();

	static DefaultListModel<Student> stulistModel = new DefaultListModel<Student>();
	static DefaultListModel<Person> perlistModel = new DefaultListModel<Person>();
	static DefaultListModel<Employee> emplistModel = new DefaultListModel<Employee>();


	//various conditionals for while loops
	static int firstRun = 0;
	/*static boolean isDefStuRemoved = false;
static boolean isDefPerRemoved = false;
static boolean isDefEmpRemoved = false;*/





	public static void main(String[] args){


		final JFrame thing = new JFrame("This is a grid'");
		thing.setLayout(new GridLayout(3,3));

		//row 1 and 2 fields added below
		//---------------------------------------------------------------------------		
		JButton addPerson = new JButton("Add Person");
		JButton addStudent = new JButton("Add Student");
		JButton addEmployee = new JButton("Add Employee");
		thing.add(new JLabel("Person"));
		thing.add(new JLabel("Student"));
		thing.add(new JLabel("Employee"));
		//end row 1 and 2 fields here
		//---------------------------------------------------------------------------		

		//Person JList declarations below		
		//----------------------------------------------------------------------------		
		//passes in default values to initialize the person array list
		
		while(firstRun == 0){
			perList = personList("First Name", "Last Name");
			firstRun++;
		}		

		final JList<Person> PersonList = new JList<Person>(perlistModel); 
		MouseAdapter PerListListener = new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {

					perAlertWindow(PersonList.getSelectedValue());

				}
			}
		};
		PersonList.addMouseListener(PerListListener);
		
		
		//Student JList declarations below		
		//----------------------------------------------------------------------------		
		//passes in default values to initialize the student array list
		while(firstRun == 1){
			stuList = studentList("First Name", "Last Name", "Student ID");
			firstRun++;

		}

		final JList<Student> StudentList = new JList<Student>(stulistModel); 
		MouseAdapter StuListListener = new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {

					stuAlertWindow(StudentList.getSelectedValue());
				}
			}
		};
		StudentList.addMouseListener(StuListListener);
		//Employee JList declarations below		
		//----------------------------------------------------------------------------		
		//passes in default values to initialize the employee array list
		while(firstRun == 2){
			empList = employeeList("First Name", "Last Name", "Employee ID");
			firstRun++;
		}


		final JList<Employee> EmployeeList = new JList<Employee>(emplistModel); 
		MouseAdapter EmpListListener = new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					System.out.println(EmployeeList.getSelectedValue());
					empAlertWindow(EmployeeList.getSelectedValue());


				}
			}
		};
		EmployeeList.addMouseListener(EmpListListener);
		//End JList section		
		//----------------------------------------------------------------------------		


		// Add remaining fields here
		//----------------------------------------------------------------------------		
		JScrollPane PerScrollPane = new JScrollPane(PersonList);
		JScrollPane StuScrollPane = new JScrollPane(StudentList);
		JScrollPane EmpScrollPane = new JScrollPane(EmployeeList);
		thing.add(PerScrollPane);	
		thing.add(StuScrollPane);
		thing.add(EmpScrollPane);

		thing.add(addPerson);
		thing.add(addStudent);
		thing.add(addEmployee);

		//End Fields
		//-----------------------------------------------------------------------------

		//Frame configuration here		
		thing.setTitle("Object Adder");
		thing.setVisible(true);
		thing.setSize(600,600);
		//thing.pack();

		thing.setLocationRelativeTo(null);
		thing.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		addStudent.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e){
				studentGUI(thing);

			}			

		});
		addPerson.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e){
				personGUI(thing);

			}
		});
		addEmployee.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e){
				employeeGUI(thing);

			}
		});
	}//end main


	//End Configuration
	//-------------------------------------------------------------------------






	public static ArrayList<Student> studentList(String fn, String ln, String sID){



		Student tempStudent = new Student();
		Student DefaultStudent = new Student();
		
		//creates a default student to initialize the array list  if the fields contain specific values 
		if(fn == "First Name" && ln == "Last Name"  && sID == "Student ID" ){


			DefaultStudent.setFname(fn);
			DefaultStudent.setLname(ln);
			DefaultStudent.setSID(sID);
			stuList.add(DefaultStudent);

		}

		else{
			tempStudent.setFname(fn);
			tempStudent.setLname(ln);
			tempStudent.setSID(sID);
			stuList.add(tempStudent);
			stuList.remove(0);
	

		}

		return stuList;
	}






	public static ArrayList<Person> personList(String fn, String ln){





		Person tempPerson = new Person();
		Person DefaultPerson = new Person();
		//creates a default person to initialize the array list if the fields contain specific values 
		if(fn == "First Name" && ln == "Last Name"){

			DefaultPerson.setFname(fn);
			DefaultPerson.setLname(ln);
			perList.add(DefaultPerson);
		}

		else{
			tempPerson.setFname(fn);
			tempPerson.setLname(ln);
			perList.add(tempPerson);
			perList.remove(0);

		}
		return perList;
	}



	public static ArrayList<Employee> employeeList(String fn, String ln, String eID){


		Employee tempEmployee = new Employee();
		Employee DefaultEmployee = new Employee();
		//creates a default employee to initialize the array list if the fields contain specific values 
		if(fn == "First Name" && ln == "Last Name"  && eID == "Employee ID" ){

			DefaultEmployee.setFname(fn);
			DefaultEmployee.setLname(ln);
			DefaultEmployee.setEID(eID);
			empList.add(DefaultEmployee);

		}

		else{
			tempEmployee.setFname(fn);
			tempEmployee.setLname(ln);
			tempEmployee.setEID(eID);
			empList.add(tempEmployee);
			empList.remove(0);//removes the previous entry to prevent duplicate entries


		}
		return empList;

	}

	//--------------------------------------------------------------------------------------------------
	//student gui here
	public static void studentGUI(JFrame student){


		final JTextField Fname = new JTextField();
		final JTextField Lname = new JTextField();
		final JTextField SID = new JTextField();




		final JFrame frame = new JFrame();
		frame.setSize(750, 375);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new GridLayout(4,1));
		frame.add(new JLabel("First Name"));
		frame.add(Fname);
		frame.add(new JLabel("Last Name"));
		frame.add(Lname);
		frame.add(new JLabel("Student ID"));

		frame.add(SID);




		JButton submit = new JButton("Submit");
		frame.add(submit);
		JButton cancel = new JButton("Cancel");
		frame.add(cancel);
		frame.pack();
		submit.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e){

				String Fnametxt = Fname.getText();
				String Lnametxt = Lname.getText();
				String SIDtxt = SID.getText();
								
				//variables for input check
				Integer checkFname;
				@SuppressWarnings("unused")
				Integer checkSid;
				Integer checkLname;
				
				
				studentList(Fnametxt, Lnametxt, SIDtxt);//calls student array list method
				//iterates through the array list and adds the Objects to the model
				Iterator<Student> ir = stuList.iterator();
				while (ir.hasNext()){
					stulistModel.addElement(ir.next());
									
				}
				
				
				try{//checks for proper input, if found alerts the user but submits the data anyway
					checkFname = Integer.parseInt(Fnametxt);
					checkLname = Integer.parseInt(Lnametxt);

					if(checkFname instanceof Number)
						JOptionPane.showMessageDialog(frame, "Does" +  Fnametxt + "look like a name found on our Planet? Fine I will just submit it anyway");
					if(checkLname instanceof Number)
						JOptionPane.showMessageDialog(frame, "Does" +  Lnametxt + "look like a name found on our Planet? Fine I will just submit it anyway");

				}
				catch(NumberFormatException x){



				}
				try{
					checkSid = Integer.parseInt(SIDtxt);

				}
				catch(NumberFormatException x){
					JOptionPane.showMessageDialog(frame, SIDtxt + "Is not in the proper format for this aplication. Fine I will just submit it anyway");
				}



				frame.setVisible(false);




			}});
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){

				frame.setVisible(false);


			}
		});


	}
	//--------------------------------------------------------------------------------------------------
	//person gui here

	public static void personGUI(JFrame person){
		final JTextField Fname = new JTextField();
		final JTextField Lname = new JTextField();

		final JFrame frame = new JFrame();
		frame.setSize(750, 375);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new GridLayout(3,1));
		frame.add(new JLabel("First Name"));
		frame.add(Fname);
		frame.add(new JLabel("Last Name"));
		frame.add(Lname);

		JButton submit = new JButton("Submit");
		frame.add(submit);
		JButton cancel = new JButton("Cancel");
		frame.add(cancel);
		frame.pack();
		submit.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e){

				String Fnametxt = Fname.getText();
				String Lnametxt = Lname.getText();
				
				//variables for input check
				Integer checkFname;
				Integer checkLname;
				
				personList(Fnametxt, Lnametxt);//calls person array list method
				
				//iterates through the array list and adds the Objects to the model
				Iterator<Person> ir = perList.iterator();
				while (ir.hasNext()){
					perlistModel.addElement(ir.next());

				}
				try{//checks for proper input, if found alerts the user but submits the data anyway


					checkFname = Integer.parseInt(Fnametxt);
					checkLname = Integer.parseInt(Lnametxt);


					if(checkFname instanceof Number)
						JOptionPane.showMessageDialog(frame, "The First Name field a Number but oh well");
					if(checkLname instanceof Number)
						JOptionPane.showMessageDialog(frame, "The Last Name field is a Number but oh well");
					frame.setVisible(false);


				}catch(NumberFormatException x){}
				frame.setVisible(false);

			}
		});
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){

				frame.setVisible(false);

			}
		});
	}
	//--------------------------------------------------------------------------------------------------
	//Employee gui here

	public static void employeeGUI(JFrame employee){
		final JTextField Fname = new JTextField();
		final JTextField Lname = new JTextField();
		final JTextField EID = new JTextField();




		final JFrame frame = new JFrame();
		frame.setSize(750, 375);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new GridLayout(4,1));
		frame.add(new JLabel("First Name"));
		frame.add(Fname);
		frame.add(new JLabel("Last Name"));
		frame.add(Lname);
		frame.add(new JLabel("Employee ID"));

		frame.add(EID);




		JButton submit = new JButton("Submit");
		frame.add(submit);
		JButton cancel = new JButton("Cancel");
		frame.add(cancel);
		frame.pack();
		submit.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e){

				String Fnametxt = Fname.getText();
				String Lnametxt = Lname.getText();
				String EIDtxt = EID.getText();

				//variabls for input chexk
				Integer checkFname;
				Integer checkLname;
				@SuppressWarnings("unused")
				Integer checkEid;

				employeeList(Fnametxt, Lnametxt, EIDtxt);//calls employee array list method
				
				//iterates through the array list and adds the Objects to the model
				Iterator<Employee> ir = empList.iterator();
				while (ir.hasNext()){
					emplistModel.addElement(ir.next());

				}

				try{//checks for proper input, if found alerts the user but submits the data anyway
					checkFname = Integer.parseInt(Fnametxt);
					checkLname = Integer.parseInt(Lnametxt);

					if(checkFname instanceof Number)
						JOptionPane.showMessageDialog(frame, "Does" +  Fnametxt + "look like a name found on our Planet? Fine I will just submit it anyway");
					if(checkLname instanceof Number)
						JOptionPane.showMessageDialog(frame, "Does" +  Lnametxt + "look like a name found on our Planet? Fine I will just submit it anyway");

				}
				catch(NumberFormatException x){



				}
				try{
					checkEid = Integer.parseInt(EIDtxt);

				}
				catch(NumberFormatException x){
					JOptionPane.showMessageDialog(frame, EIDtxt + "Is not in the proper format for this aplication. Fine I will just submit it anyway");
				}
				
				
				
				frame.setVisible(false);

			}});
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){

				frame.setVisible(false);

			}
		});
	}


	public static void stuAlertWindow(Student stu){



		JDialog dialog = null;
		JOptionPane optionPane = new JOptionPane();
		optionPane.setMessage("The data for the selected objec is displayed below");
		optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);

		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(3,2));

		panel.add(new JLabel("First Name"));
		panel.add(new JLabel(stu.getFname()));

		panel.add(new JLabel("Last Name"));
		panel.add(new JLabel(stu.getLname()));

		panel.add(new JLabel("Student ID"));
		panel.add(new JLabel(stu.getsID()));




		/* String[] buttonTxt = {"Need Help","Help Me","Counting"};
	    JButton[] buttons = new JButton[buttonTxt.length];
	    for (int i = 0; i < buttonTxt.length; i++)
	    {
	        buttons[i] = new JButton(buttonTxt[i]);
	        panel.add(buttons[i]);
	    }*/
		optionPane.setOptionType(JOptionPane.DEFAULT_OPTION);
		optionPane.add(panel);
		dialog = optionPane.createDialog(null, "Icon/Text Button");
		dialog.setVisible(true);



	}


	public static void perAlertWindow(Person per){



		JDialog dialog = null;
		JOptionPane optionPane = new JOptionPane();
		optionPane.setMessage("The data for the selected objec is displayed below");
		optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);

		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(3,2));

		panel.add(new JLabel("First Name"));
		panel.add(new JLabel(per.getFname()));

		panel.add(new JLabel("Last Name"));
		panel.add(new JLabel(per.getLname()));



		/* String[] buttonTxt = {"Need Help","Help Me","Counting"};
	    JButton[] buttons = new JButton[buttonTxt.length];
	    for (int i = 0; i < buttonTxt.length; i++)
	    {
	        buttons[i] = new JButton(buttonTxt[i]);
	        panel.add(buttons[i]);
	    }*/
		optionPane.setOptionType(JOptionPane.DEFAULT_OPTION);
		optionPane.add(panel);
		dialog = optionPane.createDialog(null, "Icon/Text Button");
		dialog.setVisible(true);



	}
	public static void empAlertWindow(Employee emp){



		JDialog dialog = null;
		JOptionPane optionPane = new JOptionPane();
		optionPane.setMessage("The data for the selected objec is displayed below");
		optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);

		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(3,2));

		panel.add(new JLabel("First Name"));
		panel.add(new JLabel(emp.getFname()));

		panel.add(new JLabel("Last Name"));
		panel.add(new JLabel(emp.getLname()));

		panel.add(new JLabel("Employee ID"));
		panel.add(new JLabel(emp.geteID()));




		/* String[] buttonTxt = {"Need Help","Help Me","Counting"};
	    JButton[] buttons = new JButton[buttonTxt.length];
	    for (int i = 0; i < buttonTxt.length; i++)
	    {
	        buttons[i] = new JButton(buttonTxt[i]);
	        panel.add(buttons[i]);
	    }*/
		optionPane.setOptionType(JOptionPane.DEFAULT_OPTION);
		optionPane.add(panel);
		dialog = optionPane.createDialog(null, "Icon/Text Button");
		dialog.setVisible(true);



	}








}




































































